package com.itheima.tlias.controller;

import com.itheima.tlias.annotation.LogOperation;
import com.itheima.tlias.service.DeptService;
import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 05 4月 2025 10:34
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        log.info("查询所有部门信息");
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }


    /**
     * 删除部门信息
     */
    @LogOperation
    @DeleteMapping
//    public Result delete(Integer id){
    public Result delete(@RequestParam("id") Integer uid){
//    public Result delete(@RequestParam(value = "id", required = false) Integer id) {
        log.info("删除部门信息：{}",uid);
        deptService.delete(uid);
        return Result.success();
    }

    /**
     * 添加部门信息
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门信息：{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    /**
     * 根据id修改部门信息
     * 先查询获取到原来的数据回显数据
     */
    @GetMapping("/{id}")
//    public Result findById(@PathVariable Integer id){   // 后面全部都用id
    public Result findById(@PathVariable("id") Integer uid){  // 后面全部都用uid
//        Dept dept = deptService.findById(id);
        log.info("根据id查询部门信息：{}",uid);
        Dept dept = deptService.findById(uid);
        return Result.success(dept);
    }

    /**
     * 根据id修改部门信息
     * 修改部门信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门信息：{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
